package cn.tanghom.baiding.service.system.service;

import cn.tanghom.baiding.api.system.model.Organization;
import cn.tanghom.baiding.api.system.service.OrganizationService;
import cn.tanghom.baiding.core.service.impl.BaseServiceImpl;
import cn.tanghom.baiding.core.tree.SelectTreeUtil;
import cn.tanghom.baiding.core.tree.SimpleJsonTree;
import cn.tanghom.baiding.core.tree.TreeObject;
import cn.tanghom.baiding.core.utils.CommonUtils;
import cn.tanghom.baiding.core.utils.JsonUtil;
import cn.tanghom.baiding.service.system.dao.OrganizationDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 组织架构业务逻辑处理层实现类
 * @author: NEMO
 * @date: 2017-07-10 16:28
 */
@Service("organizationService")
public class OrganizationServiceImpl extends BaseServiceImpl<Organization> implements OrganizationService{

    @Resource
    private OrganizationDao organizationDao;

    @Override
    public String saveOrUpdate(Organization organization) {
        try {
            Organization parentOrg = new Organization();
            if(organization.getParentId()==0){
                parentOrg.setId(0L);
                parentOrg.setParentName("无上级");
            }else{
                parentOrg = organizationDao.selectByPrimaryKey(organization.getParentId());
                organization.setParentId(parentOrg.getId());
                organization.setParentName(parentOrg.getParentName());
            }
            if (organization.getId() == null) {// 添加
                organizationDao.insert(organization);
            } else {// 修改
                Organization org = organizationDao.selectByPrimaryKey(organization.getId());
                org.setName(organization.getName());
                org.setDescription(organization.getDescription());
                org.setParentId(parentOrg.getId());
                org.setParentName(parentOrg.getParentName());
                org.setStatus(organization.getStatus());
                org.setSort(organization.getSort());
                organizationDao.update(org);
            }
            return CommonUtils.msgCallback(true, "操作成功", "", JsonUtil.toJSONString(organization));
        } catch (Exception e) {
            throw e;
        }
    }

    @Override
    public List<Organization> selectList() {
        return organizationDao.selectListByParams(null);
    }

    @Override
    public List<Organization> selectListByParentId(Long parentId) {
        return organizationDao.selectListByParentId(parentId,1);
    }

    @Override
    public List<TreeObject> selectToSelectTree() {
        List<Organization> organizations = organizationDao.selectListByParams(null);
        List<TreeObject> list = new ArrayList<TreeObject>();
        for (Organization org : organizations) {
            TreeObject ts = new TreeObject();
            ts.setId(org.getId().intValue());
            ts.setName(org.getName());
            ts.setParentId(org.getParentId().intValue());
            list.add(ts);
        }
        SelectTreeUtil selectTreeUtil = new SelectTreeUtil();
        List<TreeObject> orgTree = selectTreeUtil.listToTree(list, 0);
        return orgTree;
    }

    @Override
    public String selectListToSimpleJson(Integer status) {
        List<Organization> organizations = organizationDao.selectListByParams(null);
        List<SimpleJsonTree> simpleJsonTreeList = new ArrayList<SimpleJsonTree>();
        if (null != organizations) {
            for (int i = 0; i < organizations.size(); i++) {
                Organization org = organizations.get(i);
                Integer id = org.getId()!=null? org.getId().intValue():0;
                Integer pId = org.getParentId()!=null? org.getParentId().intValue():0;
                SimpleJsonTree simpleJsonTree = new SimpleJsonTree(id, pId, org.getName());
                simpleJsonTreeList.add(simpleJsonTree);
            }
        }
        return JsonUtil.toJSONString(simpleJsonTreeList);
    }
}
